package com.itheima.dao;

import com.itheima.pojo.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;

import java.util.List;
import java.util.Map;

public interface OrderDao {

    @Select("select * from t_order where member_id=#{memberId} and orderDate=#{orderDate} and setmeal_id=#{setmealId}")
    Order findByCondition(Order order);

    @Insert("insert into t_order(id,member_id,orderDate,orderType,orderStatus,setmeal_id) values(#{id},#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId})")
    @SelectKey(statement = "select last_insert_id()", before = false, keyProperty = "id", resultType = Integer.class, keyColumn = "id")
    void add(Order order);

    @Select("SELECT o.orderDate orderDate, o.orderType orderType, m. NAME member, s. NAME setmeal FROM t_order o, t_member m, t_setmeal s WHERE o.id = #{id} AND o.member_id = m.id AND o.setmeal_id = s.id")
    Map findById(Integer id);

    @Select("select m.name member ,s.name setmeal,o.orderDate orderDate,o.orderType orderType from t_order o, t_member m, t_setmeal s where o.member_id=m.id and o.setmeal_id=s.id and o.id=#{id}")
    public Map findById4Detail(Integer id);

    @Select("select count(id) from t_order where orderDate=#{date}")
    public Integer findOrderCountByDate(String date);

    @Select("select count(id) from t_order where orderDate>#{date}")
    public Integer findOrderCountAfterDate(String date);

    @Select("select count(id) from t_order where orderDate=#{date} and orderStatus='已到诊'")
    public Integer findVisitsCountByDate(String date);

    @Select("select count(id) from t_order where orderDate>#{date} and orderStatus='已到诊'")
    public Integer findVisitsCountAfterDate(String date);

    @Select("select (select name from t_setmeal where id= setmeal_id) name, count(id) setmeal_count, count(id)/(select count(id) from t_order) proportion from t_order group by setmeal_id order by setmeal_count desc limit 4;")
    public List<Map> findHotSetmeal();

    @Select("select * from t_order where id=#{id}")
    public Order findStatusById(Integer id);
}


